﻿@* Generator: MvcView *@
@using PagedList
@using PagedList.Mvc
@model Plugin_DisplayFavorites.Favorites
@{
    var url = Request.RawUrl;
    int i=url.IndexOf("?");
    if (i > 0) { 
        url = url.Substring(0, i);
        }
    var urlAdd =url+ "/Add";
    var urlRemove = url + "/Remove";
           
}
<h1>Existing favorites</h1>
    <ul>
@foreach (var item in Model.favoritesList)
{ 
    <li>@item.LinkName <a href="javascript:UnSelect(@item.LinkID)">remove</a> </li>
}
    </ul>


    <h1>Select Links</h1>
    <ul>
@foreach (var item in Model.linkSearch)
{ 
    <li><a href="javascript:Select(@item.LinkID)">add</a> @item.LinkName</li>
}
</ul>
 @if (Model.linkSearch.PageCount > 1)
 { 
        @Html.PagedListPager(Model.linkSearch, page => Url.Action("Index", new { page, pageSize = 10 }), PagedListRenderOptions.PageNumbersOnly)
    }
    
    <script type="text/javascript">
        function UnSelect(Theid) {
            $.ajax({
                type: "POST",
                url: "@urlRemove",
                data: { id: Theid },
                datatype: "JSON",
                success: function (returndata) {
                    if (returndata.ok) {
                        window.location = window.location;
                    }
                    else {
                        //this is an error from the server
                        window.alert(' error : ' + returndata.message);
                    }

                }
            }
        );

        }
        function Select(Theid) {

            $.ajax({
                type: "POST",
                url: "@urlAdd",
                data: { id: Theid },
                datatype: "JSON",
                success: function (returndata) {
                    if (returndata.ok) {
                        window.location = window.location;
                    }
                    else {
                        //this is an error from the server
                        window.alert(' error : ' + returndata.message);
                    }

                }
            }
        );
            
        }
    </script>